package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Override
    @Transactional
    public void saveSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.saveSetmael(setmeal);

        //获取insert语句获取到的id
        Long setmealId=setmeal.getId();

        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        for(SetmealDish setmealDish:setmealDishes){
            setmealDish.setSetmealId(setmealId);
            setmealMapper.saveSetmealDish(setmealDish);
        }

    }

    @Override
    public PageResult getSetealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.getSetealPage(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        //判断套餐是否在售
        for (Long id : ids) {
            Setmeal setmeal=setmealMapper.getById(id);
            if (setmeal.getStatus()==1){
                //当前菜品处于起售中（抛出异常）
                throw new DeletionNotAllowedException("菜品在售");
            }
        }
        //删除套餐表中的数据
        //sql:delete from setmeal where id=(?,?,?,?..)
        setmealMapper.deleteByIds(ids);
        //删除套餐菜品表中华当前套餐id对应的菜品
        //sql:delete from setmeal_dish where setmeal_id=(?,?,?,?..)
        setmealMapper.deleteBySetmealIds(ids);

    }

    @Override
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealVO=new SetmealVO();
        Setmeal setmeal=setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        List<SetmealDish> setmealDishes=setmealMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        //删除当前套餐id对应的菜品
        setmealMapper.deleteBySetmealId(setmeal.getId());
        //重新添加菜品id
        for(SetmealDish setmealDish:setmealDishes){
            setmealDish.setSetmealId(setmeal.getId());
            setmealMapper.saveSetmealDish(setmealDish);
        }
        setmealMapper.update(setmeal);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        Setmeal setmeal=new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }
}
